
package com.rt.schedulenew.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rt.schedulebase.dto.DictExamQueueDto;
import com.rt.schedulebase.dto.DictHospitalDto;
import com.rt.schedulebase.entity.DictHospital;
import com.rt.schedulebase.entity.DictScheduleHospital;
import com.rt.schedulebase.mapper.*;
import com.rt.schedulenew.api.IDictSerialService;
import com.rt.schedulenew.api.IDictHospitalService;
import com.rt.schedulenew.service.IDictScheduleHospitalService;
import com.rt.schedulenew.utils.base.CommonUtil;
import com.rt.schedulenew.utils.base.ConstantsUtil;
import com.rt.schedulenew.utils.global.JsonResult;
import com.rt.schedulenew.utils.global.JsonResultUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DictHospitalServiceImpl extends ServiceImpl<DictHospitalMapper, DictHospital> implements IDictHospitalService {
    @Autowired
    DictHospitalMapper dictHospitalMapper;

    @Autowired
    DictExamQueueMapper dictExamQueueMapper;

    @Autowired
    ExamApplyItemMapper examApplyItemMapper;

    @Autowired
    DictExamItemMapper dictExamItemMapper;

    @Autowired
    ConfigServiceImpl configServiceImpl;

    @Autowired
    ExamApplyMapper examApplyMapper;

    @Autowired
    IDictScheduleHospitalService dictScheduleHospitalService;

    @Autowired
    IDictSerialService dictSerialService;

    @Override
    public DictHospital getHospital(String hospitalCode) {
        Map<String, Object> map = new HashMap<>();
        map.put("HOSPITAL_CODE", hospitalCode);
        QueryWrapper<DictHospital> queryWrapper = new QueryWrapper<>();
        queryWrapper.allEq(map, false);
        return dictHospitalMapper.selectOne(queryWrapper);
    }

    /***
     * 获取可以预约的院区列表
     * @param dto
     * @return
     */
    @Override
    public List<DictHospitalDto> getHospitalList(DictHospitalDto dto) {
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isNotBlank(dto.getHospitalCode())) {
            map.put("HOSPITAL_CODE", dto.getHospitalCode());
        }
        QueryWrapper<DictHospital> queryWrapper = new QueryWrapper<>();
        queryWrapper.allEq(map, false);
        queryWrapper.orderByAsc("SORT_NO");
        List<DictHospital> allList = dictHospitalMapper.selectList(queryWrapper);
        // 如果申请单号不为空
        // 查询可以使用的申请单号
        if (StringUtils.isNotBlank(dto.getApplyNo())) {
            return getEnableHospital(dto.getApplyNo());
        }
        // 获取预约医院字典表
        List<DictScheduleHospital> hospitals = dictScheduleHospitalService.getListAll();
        List<DictHospitalDto> resultList = new ArrayList<>();

        for (DictHospital dictHospital : allList) {
            for (DictScheduleHospital dictScheduleHospital : hospitals) {
                if (dictHospital.getHospitalCode().equals(dictScheduleHospital.getHospitalCode())) {
                    DictHospitalDto tem = new DictHospitalDto();
                    try {
                        BeanUtils.copyProperties(tem, dictHospital);
                    } catch (Exception ignored) {
                    }
                    resultList.add(tem);
                }
            }
        }

        if (CollectionUtils.isNotEmpty(resultList)) {
            return resultList;
        }

        resultList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(allList)) {
            for (DictHospital dictHospital : allList) {
                DictHospitalDto tem = new DictHospitalDto();
                try {
                    BeanUtils.copyProperties(tem, dictHospital);
                } catch (Exception ignored) {
                }
                resultList.add(tem);
            }
        }
        return resultList;
    }

    @Override
    public List<DictHospital> getHospitalListAll(DictHospitalDto dto) {
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isNotBlank(dto.getHospitalCode())) {
            map.put("HOSPITAL_CODE", dto.getHospitalCode());
        }
        if (StringUtils.isNotBlank(dto.getHospitalName())) {
            map.put("HOSPITAL_NAME", dto.getHospitalName());
        }
        QueryWrapper<DictHospital> queryWrapper = new QueryWrapper<>();
        queryWrapper.allEq(map, false);
        queryWrapper.orderByAsc("SORT_NO");
        return dictHospitalMapper.selectList(queryWrapper);
    }

    /**
     * 根据申请单号获取可用的院区列表
     *
     * @param applyNo
     * @return
     */
    private List<DictHospitalDto> getEnableHospital(String applyNo) {
        List<String> applyNoList = CommonUtil.split2List(applyNo);
        QueryWrapper<DictHospital> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("SORT_NO");
        List<DictHospital> hospitalList = dictHospitalMapper.selectList(queryWrapper);

        List<DictHospitalDto> hospitalDtoList = new ArrayList<DictHospitalDto>();
        List<String> examApplyItemList = examApplyItemMapper.getExamItemByApplyNo(applyNoList);
        List<String> spanHospitalExamClass = configServiceImpl.getConfigListByName(ConstantsUtil.ConfigName.SPAN_HOSPITAL_EXAM_CLASS.getKey());

        if (spanHospitalExamClass != null && !spanHospitalExamClass.isEmpty()) {
            for (DictHospital hospital : hospitalList) {
                DictHospitalDto dictHospitalDto = new DictHospitalDto();
                dictHospitalDto.setHospitalCode(hospital.getHospitalCode());
                dictHospitalDto.setHospitalName(hospital.getHospitalName());
                dictHospitalDto.setApplyNoList(applyNoList);
                dictHospitalDto.setMailingAddress(hospital.getMailingAddress());
                List<DictExamQueueDto> examQueueList = dictExamQueueMapper.getHospitalEnableQueue(dictHospitalDto);
                if (examQueueList.size() > 0) {
                    if (spanHospitalExamClass.contains(examQueueList.get(0).getExamClass())) {
                        Map<String, Object> map = new HashMap<String, Object>();
                        map.put("applyNoList", applyNoList);
                        map.put("examClass", examQueueList.get(0).getExamClass());
                        int enableNum = dictExamItemMapper.getHospitalEnableItem(map);
                        if (enableNum >= examApplyItemList.size()) {
                            dictHospitalDto.setIsEnable("1");
                        } else {
                            dictHospitalDto.setIsEnable("0");
                        }
                    } else {
                        dictHospitalDto.setIsEnable("1");
                    }
                } else {
                    dictHospitalDto.setIsEnable("0");
                }
                dictHospitalDto.setApplyNoList(null);
                dictHospitalDto.setHospitalAlias(hospital.getHospitalAlias());
                hospitalDtoList.add(dictHospitalDto);
            }
        } else {
            List<String> performHospital = examApplyMapper.getPerformHospital(applyNoList);
            for (DictHospital hospital2 : hospitalList) {
                if (hospital2.getHospitalCode().equals(performHospital.get(0))) {
                    DictHospitalDto dictHospitalDto2 = new DictHospitalDto();
                    dictHospitalDto2.setHospitalCode(hospital2.getHospitalCode());
                    dictHospitalDto2.setHospitalName(hospital2.getHospitalName());
                    dictHospitalDto2.setMailingAddress(hospital2.getMailingAddress());
                    dictHospitalDto2.setIsEnable("1");
                    dictHospitalDto2.setHospitalAlias(hospital2.getHospitalAlias());
                    hospitalDtoList.add(dictHospitalDto2);
                }
            }
        }
        return hospitalDtoList;
    }

    @Override
    public List<DictHospital> selectList(DictHospitalDto dto) {
        Map<String, Object> map = new HashMap<String, Object>();
        QueryWrapper<DictHospital> qw = new QueryWrapper<>();
        map.put("HOSPITAL_CODE", dto.getHospitalCode());
        qw.allEq(map, false);

        return dictHospitalMapper.selectList(qw);
    }

    @Override
    public JsonResult insertHospital(DictHospitalDto dto) {
        if (StringUtils.isBlank(dto.getHospitalName())) {
            return JsonResultUtil.failure("医院名称不能为空");
        }
        QueryWrapper<DictHospital> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("HOSPITAL_NAME", dto.getHospitalName());
        int result = dictHospitalMapper.selectCount(queryWrapper);
        if (result > 0) {
            return JsonResultUtil.failure("新增失败:医院名称已存在");
        }

        // 没有填写医院代码 就自动生成一条个id
        if (StringUtils.isEmpty(dto.getHospitalCode())) {
            String id = dictSerialService.getSerialId("HOSPITAL_CODE");
            dto.setHospitalCode(id);
        }

        int row = dictHospitalMapper.insert(dto);
        if (row > 0) {
            return JsonResultUtil.success("新增成功");
        }
        return JsonResultUtil.failure("新增失败");
    }

    @Override
    public JsonResult updateHospital(DictHospitalDto dto) {
        if (StringUtils.isBlank(dto.getHospitalCode())) {
            return JsonResultUtil.failure("医院代码不能为空");
        }
        UpdateWrapper<DictHospital> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("HOSPITAL_CODE", dto.getHospitalCode());
        int row = dictHospitalMapper.update(dto, updateWrapper);
        if (row > 0) {
            return JsonResultUtil.success("修改成功");
        }
        return JsonResultUtil.failure("修改失败");
    }

    @Override
    public JsonResult deleteHospital(DictHospitalDto dto) {
        if (StringUtils.isBlank(dto.getHospitalCode())) {
            return JsonResultUtil.failure("医院代码不能为空");
        }
        QueryWrapper<DictHospital> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("HOSPITAL_CODE", dto.getHospitalCode());
        int row = dictHospitalMapper.delete(queryWrapper);
        if (row > 0) {
            return JsonResultUtil.success("删除成功");
        }
        return JsonResultUtil.failure("删除失败");
    }
}
